import { http } from '@/utils/request'
import type { ApiResponse, PaginationResult } from '@/types'

/**
 * 项目相关API
 */
export const projectApi = {
  // 获取项目列表
  getProjects(params: {
    page?: number
    page_size?: number
    search?: string
    type?: string
    difficulty?: string
    sort?: string
    status?: string
  } = {}): Promise<ApiResponse<PaginationResult<{
    id: number
    title: string
    description: string
    type: string
    difficulty: string
    status: string
    cover_image?: string
    tech_stack: string[]
    like_count: number
    view_count: number
    author: {
      id: number
      username: string
      avatar_url?: string
    }
    created_at: string
    updated_at: string
  }>>> {
    return http.get('/v1/projects/', { params })
  },

  // 获取项目详情
  getProjectDetail(projectId: number): Promise<ApiResponse<{
    id: number
    title: string
    description: string
    content: string
    type: string
    difficulty: string
    status: string
    cover_image?: string
    images: string[]
    tech_stack: string[]
    github_url?: string
    demo_url?: string
    like_count: number
    view_count: number
    is_liked: boolean
    is_favorited: boolean
    author: {
      id: number
      username: string
      avatar_url?: string
      title?: string
    }
    created_at: string
    updated_at: string
  }>> {
    return http.get(`/v1/projects/${projectId}`)
  },

  // 创建项目
  createProject(data: {
    title: string
    description: string
    content: string
    type: string
    difficulty: string
    tech_stack: string[]
    cover_image?: string
    images?: string[]
    github_url?: string
    demo_url?: string
  }): Promise<ApiResponse<{
    id: number
    message: string
  }>> {
    return http.post('/v1/projects', data)
  },

  // 更新项目
  updateProject(projectId: number, data: {
    title?: string
    description?: string
    content?: string
    type?: string
    difficulty?: string
    tech_stack?: string[]
    cover_image?: string
    images?: string[]
    github_url?: string
    demo_url?: string
  }): Promise<ApiResponse<{
    message: string
  }>> {
    return http.put(`/v1/projects/${projectId}`, data)
  },

  // 删除项目
  deleteProject(projectId: number): Promise<ApiResponse<{
    message: string
  }>> {
    return http.delete(`/v1/projects/${projectId}`)
  },

  // 点赞项目
  likeProject(projectId: number): Promise<ApiResponse<{
    success: boolean
    message: string
    like_count: number
  }>> {
    return http.post(`/v1/projects/${projectId}/like`)
  },

  // 取消点赞
  unlikeProject(projectId: number): Promise<ApiResponse<{
    success: boolean
    message: string
    like_count: number
  }>> {
    return http.delete(`/v1/projects/${projectId}/like`)
  },

  // 收藏项目
  favoriteProject(projectId: number): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.post(`/v1/projects/${projectId}/favorite`)
  },

  // 取消收藏
  unfavoriteProject(projectId: number): Promise<ApiResponse<{
    success: boolean
    message: string
  }>> {
    return http.delete(`/v1/projects/${projectId}/favorite`)
  },

  // 获取用户的项目
  getUserProjects(userId: number, params: {
    page?: number
    page_size?: number
    status?: string
  } = {}): Promise<ApiResponse<PaginationResult<any>>> {
    return http.get(`/v1/users/${userId}/projects`, { params })
  },

  // 获取项目统计
  getProjectStats(): Promise<ApiResponse<{
    total_projects: number
    total_likes: number
    active_users: number
    today_projects: number
  }>> {
    return http.get('/v1/projects/stats')
  },

  // 上传项目图片
  uploadProjectImage(file: File): Promise<ApiResponse<{
    url: string
    message: string
  }>> {
    const formData = new FormData()
    formData.append('image', file)
    return http.post('/v1/projects/upload-image', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 获取项目评论
  getProjectComments(projectId: number, params: {
    page?: number
    page_size?: number
  } = {}): Promise<ApiResponse<PaginationResult<{
    id: number
    content: string
    author: {
      id: number
      username: string
      avatar_url?: string
    }
    created_at: string
    like_count: number
    is_liked: boolean
  }>>> {
    return http.get(`/v1/projects/${projectId}/comments`, { params })
  },

  // 添加项目评论
  addProjectComment(projectId: number, data: {
    content: string
  }): Promise<ApiResponse<{
    id: number
    message: string
  }>> {
    return http.post(`/v1/projects/${projectId}/comments`, data)
  },

  // 点赞评论
  likeComment(commentId: number): Promise<ApiResponse<{
    success: boolean
    like_count: number
  }>> {
    return http.post(`/v1/comments/${commentId}/like`)
  },

  // 取消点赞评论
  unlikeComment(commentId: number): Promise<ApiResponse<{
    success: boolean
    like_count: number
  }>> {
    return http.delete(`/v1/comments/${commentId}/like`)
  }
}
